package web;


import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import system.RunTimeData;
import system.StaticData;
import system.bean.Message;
import system.bean.UserState;

public class InterfaceController extends AbstractControllerUtil implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		if (!validateRequest(request))
			return null;
		
		Map<String, Object> model = new HashMap<String, Object>();

		Object o = request.getSession().getAttribute(StaticData.SESSION_USERID_NAME);

		int userid = -1;
		try{
			userid = Integer.parseInt(o.toString());
		}catch(Exception e){

			e.printStackTrace();
			return null;
		}
		
		UserState myState = RunTimeData.Get(userid);
		while(myState == null){
			/*无限等待*/
			Thread.sleep(1);
		}

		
		LinkedHashSet<Message> mymessagelist = myState.getMessagelist();
		while(mymessagelist.size() < 1){
			/*无限等待*/
			Thread.sleep(1);
		}

		model.put("messages", mymessagelist.clone());
		mymessagelist.clear();
		
		return new ModelAndView("jsonView", model);
		
	}

}
